package com.wms.controller;

import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.*;
import com.wms.entity.Arrival;
import com.wms.entity.StockInRecord;
import com.wms.service.StockInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(tags = "入库管理")
@Slf4j
@RequestMapping("/api")
public class StockInController {
    @Autowired
    StockInService stockInService;

    @PostMapping("/query")
    @ApiOperation("分页查询各类入库任务")
    public ResultBean queryStockInTask(@RequestBody QueryDTO dto) {
        StockInQueryDTO query = new StockInQueryDTO(dto.getA_type()
                ,dto.getProcessIn()
                ,dto.getPurchaseStockIn()
                ,dto.getDirectIn());
        String a_type= query.getA_type();
        PageBean bean;
        Integer page = dto.getPage();
        Integer pageSize = dto.getPageSize();

        switch (a_type) {
            case "process":
                bean = stockInService.queryProcessInTask(page, pageSize,query.getProcessIn());
                break;
            case "purchase":
                bean = stockInService.queryPurchaseStockInTask(page, pageSize,query.getPurchaseStockIn());
                break;
            case "direct":
                bean = stockInService.queryDirectInTask(page,pageSize,query.getDirectIn());
                break;
            default:
                return R.fail("无效的查询类型，请使用process、purchase、direct");
        }
        return R.success("查询成功", bean);
    }

    @PostMapping("/queryDetail")
    @ApiOperation("分页查询各类入库任务详情")
    public ResultBean queryStockInDetail(@RequestBody QueryDetailDto dto){
        StockInDetailDTO query=new StockInDetailDTO(dto.getA_type(),
                dto.getDirect(),
                dto.getProcess(),
                dto.getStock(),
                dto.getPd());
        Integer page= dto.getPage();
        Integer pageSize=dto.getPageSize();
        String a_type= query.getA_type();
        PageBean bean;
        switch (a_type){
            case "direct":
                bean=stockInService.queryDirectInDetail(page,pageSize,query.getDirect());
                break;
            case "process":
                bean=stockInService.queryProcessInDetail(page,pageSize,query.getProcess());
                break;
            case "purchase":
                bean = stockInService.queryPurchaseStockInDetail(page, pageSize,query.getPd());
                break;
            default:
                return R.fail("无效的查询类型，请使用purchase、direct、或process");
        }
        return R.success("查询成功",bean);
    }
    @ApiOperation("逻辑删除指定采购任务详情")
    @PutMapping("/stockIn/deletePurchase")
    public ResultBean deletePurchaseDetail(Integer id){
        int rs=stockInService.deletePurchaseStockInDetail(id);
        return rs==1?R.success("删除成功"):R.fail("删除失败");
    }
    //查询入库记录
    @ApiOperation("入库操作(添加新的库区库存记录)")
    @PostMapping("/stockIn/add")
    public ResultBean savePQInTask(@RequestBody List<MasDTO> list){
        ResultBean rs= stockInService.addPQInTask(list);
        return rs;
    }
    @ApiOperation("添加产品/商品直接入库任务")
    @PostMapping("/directIn/add")
    public ResultBean saveDirectInTask(@RequestBody DirectInTaskDTO dto){
        int rs= stockInService.addDirectInTask(dto.getDirect(),dto.getDetails());
        return rs==1?R.success("保存成功"):R.fail("保存失败");
    }
    @ApiOperation("生成采购入库任务单")
    @PostMapping("/purchaseIn/add")
    public ResultBean savePurchaseInTask(@RequestBody Arrival arrival) {
        int rs=stockInService.addPurchaseInTask(arrival);
        return rs==1?R.success("保存成功"):R.fail("保存失败");
    }

    @ApiOperation("查询入库记录")
    @GetMapping("/query/stockInRecords")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页,默认为第1页",example = "1",dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize",value = "每页最大记录数,默认为10",example = "10",dataTypeClass = Integer.class)
    })
    public ResultBean queryStockInRecords(Integer page, Integer pageSize, RecordDTO record){
        PageBean bean=stockInService.queryStockInRecords(page,pageSize,record);
        return R.success("查询成功",bean);
    }
}
